//
//  RDViewController.m
//  ZYShang
//
//  Created by wangguangming on 15/7/29.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "RDViewController.h"
#import "ReadyDiSignle.h"
#import "Base.h"
#import "Targets.h"
#import "DetailViewController.h"
#import "OrderMessagesignle.h"
@interface RDViewController ()
{
    int _from;
    int _to;
    UIButton *shuaxinBUtton;
}
@property(nonatomic,strong)UIButton *itemButton;
@property(nonatomic,strong)UIScrollView *scroller;
@end

@implementation RDViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        {
        self.view.backgroundColor=COLOR;
        }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[ReadyDiSignle signle] addObserver:self forKeyPath:@"tatgetsArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

     
}

-(void)viewWillDisappear:(BOOL)animated
{
    
    [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"tatgetsArray"];
    
    [super viewWillDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
        //[[ReadyDiSignle signle] getRdItemWorking];
    
    _from=0;
    _to=20;
    [[ReadyDiSignle signle] getRdItemWorkingFrom:_from To:_to];
    [self setBack];
   self.title=@"配货记录";
    
    self.scroller=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.scroller.backgroundColor=COLOR;
     [self.view addSubview:self.scroller]; 
}
-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context
{
    if ([keyPath isEqualToString:@"tatgetsArray"]) 
        {
        
        [self shuju];
      
       }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)shuju
{
    

    
    [self.itemButton removeFromSuperview];
     NSLog(@"%d",[ReadyDiSignle signle].tatgetsArray.count);
    for (int i=0; i<[ReadyDiSignle signle].tatgetsArray.count; i++)
        {
       
        int row=i/2;
        Targets *targets=[ReadyDiSignle signle].tatgetsArray[i];
        NSString *string=targets.oid;
        NSString *b = [string substringFromIndex:string.length-6];
        
        self.itemButton=[[UIButton alloc] init];
        self.itemButton.frame=CGRectMake(6+(i%2)*(WIDTH/2-3),6+56*row, WIDTH/2-9, 50);
        self.itemButton.backgroundColor=[UIColor whiteColor];
        [self.itemButton setTitleColor:BLUECOLOL forState:UIControlStateNormal];
        
              self.itemButton.layer.cornerRadius=4.0;
        
        [self.itemButton setTitle:[NSString stringWithFormat:@"%@",b] forState:UIControlStateNormal];
        
        [self.itemButton setTag:110+i];
        [self.itemButton addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.scroller addSubview:self.itemButton];
     }
    self.scroller.contentSize=CGSizeMake(0, 5+56*([ReadyDiSignle signle].tatgetsArray.count/2+[ReadyDiSignle signle].tatgetsArray.count%2)+50);
    
    [shuaxinBUtton removeFromSuperview];
    shuaxinBUtton=[[UIButton alloc] initWithFrame:CGRectMake(WIDTH/4, CGRectGetMaxY(self.itemButton.frame)+5, WIDTH/2, 40)];
    
        //[shuaxinBUtton setTitle:@"加载更多" forState:UIControlStateNormal];
    [shuaxinBUtton setImage:[UIImage imageNamed:@"moreios.png"] forState:UIControlStateNormal];
    
    shuaxinBUtton.layer.cornerRadius=6;
    shuaxinBUtton.layer.borderColor=[[UIColor whiteColor] CGColor];
    shuaxinBUtton.layer.borderWidth=2.0;
        //shuaxinBUtton.backgroundColor=[UIColor redColor];
    [shuaxinBUtton addTarget:self action:@selector(shuaxinAction) forControlEvents:UIControlEventTouchUpInside];
    [self.scroller addSubview:shuaxinBUtton];
}
-(void)shuaxinAction
{
   
    
    if (_to>[ReadyDiSignle signle].tatgetsArray.count) 
        {
        UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"没有了" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
        
        [a show];
        
        return;
         }
    else
        {
        _to+=20;
    [[ReadyDiSignle signle] getRdItemWorkingFrom:_from To:_to];
        }
    
}
-(void)turnAction:(UIButton *)sender
{

        Targets *targets=[ReadyDiSignle signle].tatgetsArray[sender.tag-110];
        [OrderMessagesignle single].oidString=targets._id;
      
      [[OrderMessagesignle single] orderMessagworking];
    
    
    DetailViewController *detailViewController=[[DetailViewController alloc] init];
    
    [self.navigationController pushViewController:detailViewController animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
